local readScore = 0

local function isFullHouse(table)
    if #table < 5 then return false end
    -- 统计每个前缀的出现次数
    local prefixCount = {}
    for _, num in ipairs(table) do
        local prefix = math.floor(num / 10)
        prefixCount[prefix] = (prefixCount[prefix] or 0) + 1
    end

    -- 检查是否存在恰好3次的前缀
    local hasThree = false
    local hasTwo = false
    local threePrefix = nil  -- 记录出现3次的前缀，用于排除相同前缀和计算score
    for prefix, count in pairs(prefixCount) do
        if count == 3 then
            hasThree = true
            threePrefix = prefix
            break
        end
    end

    -- 如果没有出现3次的前缀，直接返回false
    if not hasThree then
        return false
    end

    -- 检查是否存在恰好2次的其他前缀
    local twoPrefix = nil
    for prefix, count in pairs(prefixCount) do
        if count == 2 and prefix ~= threePrefix then
            hasTwo = true
            twoPrefix = prefix
            break
        end
    end

    local fullHouse = hasThree and hasTwo
    if fullHouse then
        readScore = 175 + threePrefix * 3 + twoPrefix * 2
    end

    return fullHouse
end

local function isFlush(table)
    for i = 1, #table - 4 do
        if table[i] % 10 == table[i+1] % 10 and
           table[i+1] % 10 == table[i+2] % 10 and
           table[i+2] % 10 == table[i+3] % 10 then
           readScore = 125 + math.floor(table[1] / 10) + math.floor(table[2] / 10) + math.floor(table[3] / 10) + math.floor(table[4] / 10) + math.floor(table[5] / 10)
           return true
        end
    end
    return false
end

local function isStraight(table)
    for i = 1, #table - 4 do
        local i1 = math.floor(table[i] / 10)
        local i2 = math.floor(table[i + 1] / 10)
        local i3 = math.floor(table[i + 2] / 10)
        local i4 = math.floor(table[i + 3] / 10)
        local i5 = math.floor(table[i + 4] / 10)
        if i2 == i1 + 1 and
           i3 == i2 + 1 and
           i4 == i3 + 1 and
           i5 == i4 + 1 then
           readScore = 100 + i1 + i2 + i3 + i4 + i5
           return true
        end
    end
    return false
end

local function isStraightFlush(table)
    if isStraight(table) and isFlush(table) then
        readScore = 600 + math.floor(table[1] / 10) + math.floor(table[2] / 10) + math.floor(table[3] / 10) + math.floor(table[4] / 10) + math.floor(table[5] / 10)
        return true
    end
    return false
end

local function isRoyalFlush(table)
    if isStraightFlush(table) then
        if math.floor(table[1] / 10) == 10 and math.floor(table[2] / 10) == 11 and math.floor(table[3] / 10) == 12 and math.floor(table[4] / 10) == 13 and math.floor(table[5] / 10) == 14 then
            readScore = 2000 + math.floor(table[1] / 10) + math.floor(table[2] / 10) + math.floor(table[3] / 10) + math.floor(table[4] / 10) + math.floor(table[5] / 10)
            return true
        end
    end
    return false
end

local function isFourOfaKind(table)
    if #table < 4 then return false end
    local prefixCount = {} -- 先初始化表，再操作键值。未定义的表键返回 nil，但可以安全地赋值（表支持动态扩展）
    for _, num in ipairs(table) do
        local prefix = math.floor(num / 10)
        prefixCount[prefix] = (prefixCount[prefix] or 0) + 1
        -- 当某个前缀出现4次时立即返回
        if prefixCount[prefix] == 4 then
            readScore = 400 + prefix * 4
            return true
        end
    end
    return false
end

local function isThreeOfaKind(table)
    if #table < 3 then return false end
    -- 统计每个前缀的出现次数
    local prefixCount = {}
    for _, num in ipairs(table) do
        local prefix = math.floor(num / 10)
        prefixCount[prefix] = (prefixCount[prefix] or 0) + 1
    end
    -- 检查是否存在恰好3次的前缀，且没有4次及以上的前
    local isThree = false
    for prefix, count in pairs(prefixCount) do
        if count == 3 then
            isThree = true
            readScore = 80 + prefix * 3
        elseif count >= 4 then
            return false  -- 存在4次及以上的前缀，直接返回false
        end
    end
    return isThree
end

local function isTwoPairs(table)
    if #table < 4 then return false end
    -- 统计每个前缀的出现次数
    local prefixCount = {}
    for _, num in ipairs(table) do
        local prefix = math.floor(num / 10)
        prefixCount[prefix] = (prefixCount[prefix] or 0) + 1
    end

    -- 统计恰好出现两次的前缀数量
    local pairCount = 0
    local pairValue = 0
    for prefix, count in pairs(prefixCount) do
        if count == 2 then
            pairCount = pairCount + 1
            pairValue = pairValue + prefix * 2
            if pairCount >= 2 then
                readScore = pairCount * 20 + pairValue
                return true  -- 找到两组，立即返回
            end
        end
    end
    return false
end

local function isPair(table)
    if #table < 2 then return false end
    -- 统计每个前缀的出现次数
    local prefixCount = {}
    local pair = false
    for _, num in ipairs(table) do
        local prefix = math.floor(num / 10)
        prefixCount[prefix] = (prefixCount[prefix] or 0) + 1
    end
    -- 检查是否存在恰好2次的前缀，且没有3次及以上的前缀
    for prefix, count in pairs(prefixCount) do
        if count == 2 then
            pair = true
            readScore = 20 + prefix * 2
        elseif count >= 3 then
            return false  -- 存在4次及以上的前缀，直接返回false
        end
    end
    return pair
end

local function isSingleHighCard(table)
    readScore = 10 + math.floor(table[#table] / 10)
    return true
end

local function type(table)
    if #table == 0 then return "nocard" end
    if isRoyalFlush(table) then
        return "royalflush" -- 皇家同花顺/恶魔手契
    elseif isStraightFlush(table) then
        return "straightflush" -- 同花顺/部落行军
    elseif isFullHouse(table) then
        return "fullhouse" -- 葫芦/三带二/战群
    elseif isFlush(table) then
        return "flush" -- 同花/部落
    elseif isStraight(table) then
        return "straight" -- 顺子/行军
    elseif isFourOfaKind(table) then
        return "fourofakind" -- 四条/四联
    elseif isThreeOfaKind(table) then
        return "threeofakind" -- 三条/三联
    elseif isTwoPairs(table) then
        return "twopairs" -- 两对/双二联
    elseif isPair(table) then
        return "pair" -- 对子/二联
    elseif isSingleHighCard(table) then
        return "singlehighcard" -- 单牌/高牌/独行
    end
end

local function typeChineseCharacters(table)
    if #table == 0 then return "无牌" end
    if isRoyalFlush(table) then
        return "皇家同花顺" -- 恶魔手契
    elseif isStraightFlush(table) then
        return "同花顺" -- 部落行军
    elseif isFullHouse(table) then
        return "三带二" -- 葫芦/战群
    elseif isFlush(table) then
        return "同花" -- 部落
    elseif isStraight(table) then
        return "顺子" -- 行军
    elseif isFourOfaKind(table) then
        return "四条" -- 四联
    elseif isThreeOfaKind(table) then
        return "三条" -- 三联
    elseif isTwoPairs(table) then
        return "两对" -- 双二联
    elseif isPair(table) then
        return "对子" -- 二联
    elseif isSingleHighCard(table) then
        return "单牌" -- 独行
    end
end

return {
    type = type,
    typeChineseCharacters = typeChineseCharacters,
    getReadScore = function() return readScore end
}